/**
 * 动效系统 - 情感化动效与微交互设计
 * 为Forever Love应用提供统一的动画效果
 */

// 动效配置
const ANIMATION_CONFIG = {
  // 基础动效时长
  duration: {
    fast: 200,
    normal: 300,
    slow: 500,
    extra: 800
  },
  
  // 缓动函数
  easing: {
    ease: 'ease',
    easeIn: 'ease-in',
    easeOut: 'ease-out',
    easeInOut: 'ease-in-out',
    bounce: 'cubic-bezier(0.68, -0.55, 0.265, 1.55)',
    smooth: 'cubic-bezier(0.25, 0.46, 0.45, 0.94)',
    sharp: 'cubic-bezier(0.4, 0.0, 0.2, 1)'
  },
  
  // 延迟配置
  delay: {
    none: 0,
    short: 100,
    medium: 200,
    long: 300
  }
};

// 主题相关动效配置
const THEME_ANIMATIONS = {
  his: {
    primary: '#ffc107',
    secondary: '#ff9800',
    accent: '#ffeb3b',
    glow: 'rgba(255, 193, 7, 0.3)'
  },
  hers: {
    primary: '#e91e63',
    secondary: '#9c27b0',
    accent: '#f8bbd9',
    glow: 'rgba(233, 30, 99, 0.3)'
  },
  default: {
    primary: '#667eea',
    secondary: '#764ba2',
    accent: '#a8edea',
    glow: 'rgba(102, 126, 234, 0.3)'
  }
};

/**
 * 创建动画对象
 * @param {Object} options 动画配置
 * @returns {Object} 微信小程序动画对象
 */
function createAnimation(options = {}) {
  const {
    duration = ANIMATION_CONFIG.duration.normal,
    timingFunction = ANIMATION_CONFIG.easing.easeOut,
    delay = ANIMATION_CONFIG.delay.none,
    transformOrigin = '50% 50% 0'
  } = options;
  
  return wx.createAnimation({
    duration,
    timingFunction,
    delay,
    transformOrigin
  });
}

/**
 * 页面进入动画
 * @param {string} type 动画类型
 * @returns {Object} 动画配置
 */
function pageEnterAnimation(type = 'slideUp') {
  const animation = createAnimation({
    duration: ANIMATION_CONFIG.duration.slow,
    timingFunction: ANIMATION_CONFIG.easing.easeOut
  });
  
  switch (type) {
    case 'slideUp':
      return animation.translateY(50).opacity(0).step()
        .translateY(0).opacity(1).step();
    
    case 'slideDown':
      return animation.translateY(-50).opacity(0).step()
        .translateY(0).opacity(1).step();
    
    case 'slideLeft':
      return animation.translateX(50).opacity(0).step()
        .translateX(0).opacity(1).step();
    
    case 'slideRight':
      return animation.translateX(-50).opacity(0).step()
        .translateX(0).opacity(1).step();
    
    case 'fadeIn':
      return animation.opacity(0).step()
        .opacity(1).step();
    
    case 'scaleIn':
      return animation.scale(0.8).opacity(0).step()
        .scale(1).opacity(1).step();
    
    case 'bounceIn':
      const bounceAnimation = createAnimation({
        duration: ANIMATION_CONFIG.duration.extra,
        timingFunction: ANIMATION_CONFIG.easing.bounce
      });
      return bounceAnimation.scale(0).step()
        .scale(1).step();
    
    default:
      return animation.opacity(0).step().opacity(1).step();
  }
}

/**
 * 页面退出动画
 * @param {string} type 动画类型
 * @returns {Object} 动画配置
 */
function pageExitAnimation(type = 'slideDown') {
  const animation = createAnimation({
    duration: ANIMATION_CONFIG.duration.normal,
    timingFunction: ANIMATION_CONFIG.easing.easeIn
  });
  
  switch (type) {
    case 'slideUp':
      return animation.translateY(0).opacity(1).step()
        .translateY(-50).opacity(0).step();
    
    case 'slideDown':
      return animation.translateY(0).opacity(1).step()
        .translateY(50).opacity(0).step();
    
    case 'slideLeft':
      return animation.translateX(0).opacity(1).step()
        .translateX(-50).opacity(0).step();
    
    case 'slideRight':
      return animation.translateX(0).opacity(1).step()
        .translateX(50).opacity(0).step();
    
    case 'fadeOut':
      return animation.opacity(1).step()
        .opacity(0).step();
    
    case 'scaleOut':
      return animation.scale(1).opacity(1).step()
        .scale(0.8).opacity(0).step();
    
    default:
      return animation.opacity(1).step().opacity(0).step();
  }
}

/**
 * 按钮点击动画
 * @param {string} type 动画类型
 * @returns {Object} 动画配置
 */
function buttonClickAnimation(type = 'scale') {
  const animation = createAnimation({
    duration: ANIMATION_CONFIG.duration.fast,
    timingFunction: ANIMATION_CONFIG.easing.easeOut
  });
  
  switch (type) {
    case 'scale':
      return animation.scale(0.95).step()
        .scale(1).step();
    
    case 'bounce':
      const bounceAnimation = createAnimation({
        duration: ANIMATION_CONFIG.duration.normal,
        timingFunction: ANIMATION_CONFIG.easing.bounce
      });
      return bounceAnimation.scale(0.9).step()
        .scale(1.05).step()
        .scale(1).step();
    
    case 'pulse':
      return animation.scale(1).step()
        .scale(1.1).step()
        .scale(1).step();
    
    case 'shake':
      return animation.translateX(0).step()
        .translateX(-5).step()
        .translateX(5).step()
        .translateX(-5).step()
        .translateX(0).step();
    
    case 'glow':
      // 发光效果需要配合CSS实现
      return animation.scale(1).step()
        .scale(1.02).step()
        .scale(1).step();
    
    default:
      return animation.scale(0.95).step().scale(1).step();
  }
}

/**
 * 卡片悬浮动画
 * @param {boolean} isHover 是否悬浮状态
 * @returns {Object} 动画配置
 */
function cardHoverAnimation(isHover = true) {
  const animation = createAnimation({
    duration: ANIMATION_CONFIG.duration.normal,
    timingFunction: ANIMATION_CONFIG.easing.easeOut
  });
  
  if (isHover) {
    return animation.translateY(-2).scale(1.02).step();
  } else {
    return animation.translateY(0).scale(1).step();
  }
}

/**
 * 模态框动画
 * @param {boolean} show 是否显示
 * @param {string} type 动画类型
 * @returns {Object} 动画配置
 */
function modalAnimation(show = true, type = 'scale') {
  const animation = createAnimation({
    duration: ANIMATION_CONFIG.duration.normal,
    timingFunction: show ? ANIMATION_CONFIG.easing.easeOut : ANIMATION_CONFIG.easing.easeIn
  });
  
  switch (type) {
    case 'scale':
      if (show) {
        return animation.scale(0.8).opacity(0).step()
          .scale(1).opacity(1).step();
      } else {
        return animation.scale(1).opacity(1).step()
          .scale(0.8).opacity(0).step();
      }
    
    case 'slideUp':
      if (show) {
        return animation.translateY(100).opacity(0).step()
          .translateY(0).opacity(1).step();
      } else {
        return animation.translateY(0).opacity(1).step()
          .translateY(100).opacity(0).step();
      }
    
    case 'fadeIn':
      if (show) {
        return animation.opacity(0).step()
          .opacity(1).step();
      } else {
        return animation.opacity(1).step()
          .opacity(0).step();
      }
    
    default:
      return show ? 
        animation.scale(0.8).opacity(0).step().scale(1).opacity(1).step() :
        animation.scale(1).opacity(1).step().scale(0.8).opacity(0).step();
  }
}

/**
 * 列表项动画
 * @param {number} index 列表项索引
 * @param {string} type 动画类型
 * @returns {Object} 动画配置
 */
function listItemAnimation(index = 0, type = 'slideUp') {
  const delay = index * 50; // 错开动画时间
  const animation = createAnimation({
    duration: ANIMATION_CONFIG.duration.normal,
    timingFunction: ANIMATION_CONFIG.easing.easeOut,
    delay
  });
  
  switch (type) {
    case 'slideUp':
      return animation.translateY(30).opacity(0).step()
        .translateY(0).opacity(1).step();
    
    case 'slideLeft':
      return animation.translateX(30).opacity(0).step()
        .translateX(0).opacity(1).step();
    
    case 'fadeIn':
      return animation.opacity(0).step()
        .opacity(1).step();
    
    case 'scaleIn':
      return animation.scale(0.9).opacity(0).step()
        .scale(1).opacity(1).step();
    
    default:
      return animation.translateY(30).opacity(0).step()
        .translateY(0).opacity(1).step();
  }
}

/**
 * 加载动画
 * @param {string} type 动画类型
 * @returns {Object} 动画配置
 */
function loadingAnimation(type = 'spin') {
  const animation = createAnimation({
    duration: ANIMATION_CONFIG.duration.slow,
    timingFunction: 'linear'
  });
  
  switch (type) {
    case 'spin':
      return animation.rotate(0).step()
        .rotate(360).step();
    
    case 'pulse':
      return animation.scale(1).opacity(1).step()
        .scale(1.1).opacity(0.7).step()
        .scale(1).opacity(1).step();
    
    case 'bounce':
      return animation.translateY(0).step()
        .translateY(-10).step()
        .translateY(0).step();
    
    default:
      return animation.rotate(0).step().rotate(360).step();
  }
}

/**
 * 情感化动效 - 心跳效果
 * @param {string} emotion 情感类型
 * @returns {Object} 动画配置
 */
function heartbeatAnimation(emotion = 'love') {
  const animation = createAnimation({
    duration: emotion === 'excited' ? 200 : 400,
    timingFunction: ANIMATION_CONFIG.easing.easeInOut
  });
  
  const scaleValue = {
    love: 1.1,
    excited: 1.15,
    happy: 1.08,
    calm: 1.05
  }[emotion] || 1.1;
  
  return animation.scale(1).step()
    .scale(scaleValue).step()
    .scale(1).step();
}

/**
 * 情感化动效 - 呼吸效果
 * @param {number} intensity 强度 (0-1)
 * @returns {Object} 动画配置
 */
function breathingAnimation(intensity = 0.5) {
  const animation = createAnimation({
    duration: 2000 + (intensity * 1000), // 根据强度调整呼吸频率
    timingFunction: ANIMATION_CONFIG.easing.easeInOut
  });
  
  const scaleValue = 1 + (intensity * 0.1);
  
  return animation.scale(1).opacity(0.8).step()
    .scale(scaleValue).opacity(1).step()
    .scale(1).opacity(0.8).step();
}

/**
 * 情感化动效 - 波纹效果
 * @param {string} theme 主题
 * @returns {Object} 动画配置
 */
function rippleAnimation(theme = 'default') {
  const animation = createAnimation({
    duration: ANIMATION_CONFIG.duration.extra,
    timingFunction: ANIMATION_CONFIG.easing.easeOut
  });
  
  return animation.scale(0).opacity(0.8).step()
    .scale(2).opacity(0).step();
}

/**
 * 微交互 - 拖拽反馈
 * @param {number} distance 拖拽距离
 * @param {string} direction 拖拽方向
 * @returns {Object} 动画配置
 */
function dragFeedbackAnimation(distance = 0, direction = 'horizontal') {
  const animation = createAnimation({
    duration: ANIMATION_CONFIG.duration.fast,
    timingFunction: ANIMATION_CONFIG.easing.easeOut
  });
  
  const resistance = Math.min(distance * 0.3, 50); // 阻力效果
  
  if (direction === 'horizontal') {
    return animation.translateX(resistance).step();
  } else {
    return animation.translateY(resistance).step();
  }
}

/**
 * 微交互 - 成功反馈
 * @returns {Object} 动画配置
 */
function successFeedbackAnimation() {
  const animation = createAnimation({
    duration: ANIMATION_CONFIG.duration.normal,
    timingFunction: ANIMATION_CONFIG.easing.bounce
  });
  
  return animation.scale(1).step()
    .scale(1.2).step()
    .scale(1).step();
}

/**
 * 微交互 - 错误反馈
 * @returns {Object} 动画配置
 */
function errorFeedbackAnimation() {
  const animation = createAnimation({
    duration: ANIMATION_CONFIG.duration.fast,
    timingFunction: ANIMATION_CONFIG.easing.easeInOut
  });
  
  return animation.translateX(0).step()
    .translateX(-10).step()
    .translateX(10).step()
    .translateX(-10).step()
    .translateX(0).step();
}

/**
 * 获取主题动效配置
 * @param {string} theme 主题名称
 * @returns {Object} 主题动效配置
 */
function getThemeAnimationConfig(theme = 'default') {
  return THEME_ANIMATIONS[theme] || THEME_ANIMATIONS.default;
}

/**
 * 创建序列动画
 * @param {Array} animations 动画序列
 * @returns {Promise} 动画执行Promise
 */
function createSequenceAnimation(animations = []) {
  return new Promise((resolve) => {
    let currentIndex = 0;
    
    function executeNext() {
      if (currentIndex >= animations.length) {
        resolve();
        return;
      }
      
      const { animation, duration = ANIMATION_CONFIG.duration.normal } = animations[currentIndex];
      currentIndex++;
      
      setTimeout(() => {
        executeNext();
      }, duration);
    }
    
    executeNext();
  });
}

/**
 * 创建并行动画
 * @param {Array} animations 动画数组
 * @returns {Promise} 动画执行Promise
 */
function createParallelAnimation(animations = []) {
  const promises = animations.map(({ animation, duration = ANIMATION_CONFIG.duration.normal }) => {
    return new Promise((resolve) => {
      setTimeout(resolve, duration);
    });
  });
  
  return Promise.all(promises);
}

/**
 * 动画工具函数
 */
const AnimationUtils = {
  // 基础配置
  config: ANIMATION_CONFIG,
  themeConfig: THEME_ANIMATIONS,
  
  // 创建动画
  create: createAnimation,
  
  // 页面动画
  pageEnter: pageEnterAnimation,
  pageExit: pageExitAnimation,
  
  // 交互动画
  buttonClick: buttonClickAnimation,
  cardHover: cardHoverAnimation,
  modal: modalAnimation,
  listItem: listItemAnimation,
  loading: loadingAnimation,
  
  // 情感化动效
  heartbeat: heartbeatAnimation,
  breathing: breathingAnimation,
  ripple: rippleAnimation,
  
  // 微交互
  dragFeedback: dragFeedbackAnimation,
  successFeedback: successFeedbackAnimation,
  errorFeedback: errorFeedbackAnimation,
  
  // 工具函数
  getThemeConfig: getThemeAnimationConfig,
  sequence: createSequenceAnimation,
  parallel: createParallelAnimation
};

module.exports = AnimationUtils;